/*
  * MacroKey.java
 * Copyright (C) 5/21/2010, Lhrod
 * Modified      06/02/2010
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

package Sindarin.Common;

import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 *
 * @author Francisco J. Hernández
 */
public class MacroKey implements java.io.Serializable {
    private Vector key = new Vector(4);
    private static HashMap stringKey = new HashMap();

    static {
        for (int i = 0; i <= 525; i++) {
            String tmp = KeyEvent.getKeyText(i).toUpperCase();
            if (!tmp.contains("UNKNOWN"))
                stringKey.put(tmp, new Integer(i));
        }
    }
    
    /** Creates a new instance of MacroKey */
    public MacroKey() {
    }
    
    public MacroKey(Vector key) {
        try {
            this.key = (Vector)key.clone();
        }
        catch (Exception e) {
            System.out.println(java.util.ResourceBundle.getBundle("Sindarin/Gui/config").getString("ERROR_NON_SUPPORTED_CLONE") + e);
        }
    }

    public MacroKey(int key) {
        this.key.add(new Integer(key));
    }
    
    public MacroKey(String cad) {
        this.addKey(cad);
    }

    public static int getTextKey(String cad) {
        return ((Integer)stringKey.get(cad.toUpperCase())).intValue();
    }
    
    private void addKey(int nKey) {
        key.addElement(new Integer(nKey));
    }
    
    public void addKey(String cad) {
        StringTokenizer str = new StringTokenizer(cad,"'\n\t\r");
	cad = str.nextToken().toUpperCase();
        if (cad.length() == 1)
            addKey((int)cad.charAt(0));
        else
            addKey(getTextKey(cad));
    }

    public void removeKey(int rKey) {
        key.remove(new Integer(rKey));
    }

    public void removeIndex(int rKey) {
        key.remove(rKey);
    }
    
    public int getKey(int index) {
        return ((Integer)key.elementAt(index)).intValue();
    }
    
    public int []getAllKey() {
        int []allkey = new int[key.size()];
	for (int i=0; i < key.size(); i++)
            allkey[i] = ((Integer)key.elementAt(i)).intValue();
	return allkey;
    }
    
    @Override
    public String toString() {
        String res = "'";
	if (key.size() > 0) {
            res += KeyEvent.getKeyText( ((Integer) key.elementAt(0)).intValue() );
            for (int i=1; i < key.size(); i++)
                res += "+'" + KeyEvent.getKeyText( ((Integer) key.elementAt(i)).intValue() );
	}
	res += "'";
        return res;
    }
    
    public boolean equals(MacroKey b) {
        int []bKey = b.getAllKey();
        if (bKey.length != key.size())
            return false;
        for (int i=0; i < key.size(); i++)
            if (((Integer)key.elementAt(i)).intValue() != bKey[i]) return false;
        return true;
    }
    
    public MacroKey add(MacroKey b) {
        MacroKey res = new MacroKey(key);
        int []bKey = b.getAllKey();
        for (int i=0; i < bKey.length; i++)
            res.addKey(bKey[i]);
        return res;
    }
    
    public MacroKey sub(MacroKey b) {
        MacroKey res = new MacroKey(key);
        int []bKey = b.getAllKey();
        for (int i=0; i < bKey.length; i++)
            res.removeKey(bKey[i]);
        return res;
    }
}